/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generatepage.web.GenerateHelper
   Module Description   :

   Date Created      : 2010/1/7
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generatepage.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.EventImage;

/**
 * @author feeling.wang
 * 
 */
public abstract class GenerateHelper {

	/** logger */
	private final static Logger logger = LoggerFactory.getLogger(GenerateHelper.class);

	/**
	 * 建立日期目錄
	 * 
	 * @return
	 * @throws Exception
	 */
	public static boolean mkDir(String advType) throws Exception {
		try {
			String today = FastDateFormat.getInstance(EcConstant.DEFAULT_PATTERN_DATE).format(DateUtil.getToday().getTime());
			if (!new File(ServerValue.getMidApachePath() + "eventImg" + File.separator + today).exists()) {
				FileUtils.forceMkdir(new File(ServerValue.getMidApachePath() + "eventImg" + File.separator + today));
			}
			if (!new File(ServerValue.getMidApachePath() + "eventImg" + File.separator + today + File.separator + advType)
					.exists()) {
				FileUtils.forceMkdir(new File(ServerValue.getMidApachePath() + File.separator + "eventImg" + File.separator
						+ today + File.separator + advType));
			}
		} catch (Exception e) {
			logger.info("exception : {}", e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 上传图檔
	 * 
	 * @param file
	 * @param fileName
	 * @return
	 */
	public static boolean uploadImage(EventImage eventImage, File imgFile, String advType) {
		try {
			String today = FastDateFormat.getInstance(EcConstant.DEFAULT_PATTERN_DATE).format(DateUtil.getToday().getTime());
			StringBuilder imgFilePath = new StringBuilder("");
			imgFilePath.append(ServerValue.getMidApachePath()).append("eventImg").append(File.separator).append(today)
					.append(File.separator).append(advType).append(File.separator).append(eventImage.getUuid()).append(
							eventImage.getImgType());
			FileOutputStream fos = new FileOutputStream(imgFilePath.toString());
			FileInputStream fis = new FileInputStream(imgFile);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fos.close();
			fis.close();
			imgFilePath.setLength(0);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}
